package com.dt76.smallloanbackenduser.controller.xiaohao;

import com.dt76.smallloanbackenduser.pojo.xiaohao.*;
import com.dt76.smallloanbackenduser.remote.xiaohao.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class XIndexController {

    @Resource
    private BorrowOrderRemote borrowOrderRemote;

    @Resource
    private LoanTypeRemote loanTypeRemote;

    @Resource
    private LoanProductRemote loanProductRemote;

    @Resource
    private ContractTemplateRemote hContractTemplateRemote;

    @Resource
    private ApprovalLoanRemote approvalLoanRemote;

    @Resource
    private BankAccountRemote bankAccountRemote;

    @RequestMapping("/loanmanger/query")
    public String loanManger(Model model, Integer currentPageNo, BorrowOrderQuery borrowOrderQuery) {
        //页面
        if (currentPageNo <= 0) {
            currentPageNo = 1;
        }

        if (borrowOrderQuery.getQueryBoTypeList() ==  null) {
            String[] boTyList = {"复审通过","终审通过","终审拒绝"};
            borrowOrderQuery.setQueryBoTypeList(boTyList);
        }
        System.out.println(borrowOrderQuery.toString());
        //查询条件
        model.addAttribute("borrowOrderQuery", borrowOrderQuery);
        //返回贷款类型
        model.addAttribute("LoanTypes", loanProductRemote.qryAllHLoanProduct());
        //所有订单
        model.addAttribute("borrowOrders", borrowOrderRemote.qryAllOrderByPage(currentPageNo, borrowOrderQuery).getRows());
        //页面
        model.addAttribute("page", borrowOrderRemote.qryAllOrderByPage(currentPageNo, borrowOrderQuery));
        return "pages/loanManger/loanManger";

    }

    @RequestMapping("/contractManger/query")
    public String contractManger(Model model, Integer currentPageNo, BorrowOrderQuery borrowOrderQuery) {
        //页面
        if (currentPageNo <= 0) {
            currentPageNo = 1;
        }
        List<HContractTemplate> hContractTemplates = hContractTemplateRemote.qryAll();
        //传入当前页
        if (borrowOrderQuery.getQueryBoTypeList() ==  null) {
            String[] boTyList = {"终审通过","放款审批"};
            borrowOrderQuery.setQueryBoTypeList(boTyList);
        }
        //返回贷款类型
        model.addAttribute("LoanTypes", loanProductRemote.qryAllHLoanProduct());
        //查询条件
        model.addAttribute("borrowOrderQuery", borrowOrderQuery);
        //所有带合同订单
        model.addAttribute("borrowOrders", borrowOrderRemote.qryAllContract(currentPageNo, borrowOrderQuery).getRows());
        //页面
        model.addAttribute("page", borrowOrderRemote.qryAllContract(currentPageNo,borrowOrderQuery));
        //模板
        model.addAttribute("hContractTemplates", hContractTemplates);
        return "pages/loanManger/contractManger";
    }

    @RequestMapping("/ApproveLoanManger/query")
    public String ApproveLoanManger(Model model, Integer currentPageNo, BorrowOrderQuery borrowOrderQuery) {
        //页面
        if (currentPageNo <= 0) {
            currentPageNo = 1;
        }
        //查询可以启用的所有公司账户
        List<YBankAccount> yBankAccounts = bankAccountRemote.qryAllAccount();
        //传入当前页

        if (borrowOrderQuery.getQueryBoTypeList() ==  null) {
            String[] boTyList = {"放款审批"};
            borrowOrderQuery.setQueryBoTypeList(boTyList);
        }
        //查询所有的放款方式
        List<LoanType> loanTypes = loanTypeRemote.qryLoanType();
        //返回贷款类型
        model.addAttribute("LoanTypes", loanProductRemote.qryAllHLoanProduct());
        //查询条件
        model.addAttribute("borrowOrderQuery", borrowOrderQuery);
        //所有带合同订单
        model.addAttribute("borrowOrders", approvalLoanRemote.qryAllLoanOrder(currentPageNo, borrowOrderQuery).getRows());
        //页面
        model.addAttribute("page", approvalLoanRemote.qryAllLoanOrder(currentPageNo, borrowOrderQuery));
        //银行账户列表
        model.addAttribute("yBankAccounts", yBankAccounts);
        //贷款列表
        model.addAttribute("loanTypes", loanTypes);

        return "pages/loanManger/ApproveLoanManger";
    }

    @RequestMapping("/loanhander/query")
    public String loanhander(Model model, Integer currentPageNo, BorrowOrderQuery borrowOrderQuery) {
        //页面
        if (currentPageNo <= 0) {
            currentPageNo = 1;
        }
        //传入当前页

        if (borrowOrderQuery.getQueryBoTypeList() ==  null) {
            String[] boTyList = {"放款处理","已放款"};
            borrowOrderQuery.setQueryBoTypeList(boTyList);
        }
        //返回贷款类型
        model.addAttribute("LoanTypes", loanProductRemote.qryAllHLoanProduct());
        //查询条件
        model.addAttribute("borrowOrderQuery", borrowOrderQuery);
        //符合条件订单
        model.addAttribute("borrowOrders", approvalLoanRemote.qryAllHanderOrder(currentPageNo, borrowOrderQuery).getRows());
        for (Object borrowOrder: approvalLoanRemote.qryAllHanderOrder(currentPageNo,borrowOrderQuery).getRows()){
            System.out.println(borrowOrder.toString());
        }
        //页面
        model.addAttribute("page", approvalLoanRemote.qryAllHanderOrder(currentPageNo,borrowOrderQuery));

        return "pages/loanManger/loanhander";
    }
}
